package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.aizuda.limiter.annotation.RateLimit;
import com.aizuda.limiter.strategy.IpKeyGenerateStrategy;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyue.eam.admin.api.constant.ResponseWord;
import com.pyue.eam.admin.api.entity.regi.CqTaskCostEntity;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.excel.annotation.ResponseExcel;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.common.security.util.SecurityUtils;
import com.pyue.eam.regi.service.CqTaskCostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务费用表
 *
 * @date 2023-11-17 15:33:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cqTaskCost")
@Tag(description = "cqTaskCost", name = "任务费用表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CqTaskCostController {

    private final CqTaskCostService cqTaskCostService;

    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param cqTaskCost 任务费用表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    // @PreAuthorize("@pms.hasPermission('eam_cqTaskCost_view')" )
    public R getCqTaskCostPage(@ParameterObject Page page, @ParameterObject CqTaskCostEntity cqTaskCost) {
        ApiAsserts.isTrue(cqTaskCost.getTaskId() != null, "传参错误");
        LambdaQueryWrapper<CqTaskCostEntity> wrapper = Wrappers.lambdaQuery(cqTaskCost);
        return R.ok(cqTaskCostService.page(page, wrapper));
    }


    /**
     * 通过id查询任务费用表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(cqTaskCostService.getById(id));
    }

    /**
     * 新增任务费用表
     *
     * @param cqTaskCost 任务费用表
     * @return R
     */
    @Operation(summary = "新增任务费用表", description = "新增任务费用表")
    @SysLog("新增任务费用表")
    @PostMapping
    @RateLimit(count = 1, interval = "3s", strategy = {IpKeyGenerateStrategy.TYPE}, message = ResponseWord.WAIT_TIME)
    public R save(@RequestBody CqTaskCostEntity cqTaskCost) {
        cqTaskCost.setUserAvatar(SecurityUtils.getUser().getAvatar());
        return R.ok(cqTaskCostService.save(cqTaskCost));
    }

    /**
     * 修改任务费用表
     *
     * @param cqTaskCost 任务费用表
     * @return R
     */
    @Operation(summary = "修改任务费用表", description = "修改任务费用表")
    @SysLog("修改任务费用表")
    @PutMapping
    public R updateById(@RequestBody CqTaskCostEntity cqTaskCost) {
        cqTaskCost.setUserAvatar(SecurityUtils.getUser().getAvatar());
        return R.ok(cqTaskCostService.updateById(cqTaskCost));
    }

    /**
     * 通过id删除任务费用表
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除任务费用表", description = "通过id删除任务费用表")
    @SysLog("通过id删除任务费用表")
    @DeleteMapping
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(cqTaskCostService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param cqTaskCost 查询条件
     * @param ids        导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<CqTaskCostEntity> export(CqTaskCostEntity cqTaskCost, Integer[] ids) {
        return cqTaskCostService.list(Wrappers.lambdaQuery(cqTaskCost).in(ArrayUtil.isNotEmpty(ids), CqTaskCostEntity::getId, ids));
    }
}
